#!/usr/bin/env bash

# install-mote.sh
#
# Purpose: Build and install AC2 application on motes
#
# NOTE: uses env var AC2_SOURCE


usage() {
cat << EOF
usage: $0 [- g | t | v ] [-z zone_id]

This script builds and installs AC2 application on the motes. 

e.g. To install the gateway mote:
       $ $0 -g
     To install a temperature sensor and vent controller for zone 1: 
       $ $0 -t -z 1  # temperature sensor
       $ $0 -v -z 1  # vent controller

OPTIONS:
  -t      temperature mote
  -v      vent controller
  -g      gateway mote  
  -z      zone id
  -d      Verbose

 NOTES: 
   The script uses the AC2_SOURCE environment variable. 
   export AC2_SOURCE-\"path-to-ac2-source\"

EOF
}

# Source code
SOURCE=$AC2_SOURCE

# Command-line opts
VERBOSE=0
TEMP=0
GWAY=0
VENT=0
ZONEID=

# Node ID formats
NODEID_GWAY=0
NODEID_FACTOR=16 

# Constants
VENT_FILE="zone-vents"
MAKE_MOTE="make telosb install,"

# Globals
NUM_VENTS_IN_ZONE=


verbose() {
       [ $VERBOSE -eq 1 ] && $@ || :
}


getargs() {
    verbose echo  "+++ getargs: $@"
    local zid_flag=0
    
    while getopts "tvgdz:" OPTION; do
        case $OPTION in
            h)
                usage
                exit 1
                ;;
            z)
                verbose echo  "+++ getargs: zone_id $OPTARG"
                ZONEID=$OPTARG
                zid_flag=1
                ;;
            t)
                verbose echo  "+++ getargs: temperature mote"
                TEMP=1
                ;;
            g)
                verbose echo  "+++ getargs: gateway mote"
                GWAY=1
                ;;
            v)
                verbose echo  "+++ getargs: vent controller"
                VENT=1
                ;;
            d)
                VERBOSE=1
                verbose echo  "+++ getargs: verbose=1"
                ;;
            ?)
                usage
                exit
                ;;
        esac
    done

    if [[ $TEMP -eq 0 && $GWAY -eq 0 && $VENT -eq 0 ]]; then 
        usage
        exit 1
    fi

    if [ $(($TEMP+$GWAY+$VENT)) -ne 1 ]; then  # one mote at a time
        usage
        exit 1
    fi
        
    if [[ $zid_flag -eq 0 && $GWAY -eq 0 ]]; then
        echo -e "\nA zone_id is required for non-gateway motes\n"
        usage
        exit 1
    fi
}


init() {
    local vent_file

    if [ -z $AC2_SOURCE ]; then 
        echo "You need to export AC2_SOURCE=\"path-to-source\" before running this script."
        exit 1
    fi 
    
    if [ ! -d $SOURCE ]; then
        echo "ERROR: Cannot find source directory \"$SOURCE\"."

        exit 1 
    fi 
    verbose echo  "+++ init: source dir=$SOURCE" 
    
    if [ $VENT -eq 1 ]; then 
        verbose echo  "+++ init: vent=1" 
        
        vent_file="$VENT_FILE-$ZONEID"
        export NUM_VENTS_IN_ZONE=`ls | grep $vent_file | wc -l | awk '{print $1}'`
        if [ $NUM_VENTS_IN_ZONE -eq $NODEID_FACTOR ]; then
          echo "ERROR: Cannot add more vent controller to this zone. (max = $NODEID_FACTOR)"  
          exit 1
        fi
        touch "$vent_file-$((NUM_VENTS_IN_ZONE+1))"
        
        verbose echo  "+++ init: NUM_VENTS_IN_ZONE=$NUM_VENTS_IN_ZONE" 
    fi
}

main() {
    getargs $@
    init
    
    cd $SOURCE
    if [ $GWAY -eq 1 ]; then # gateway mote
        verbose echo  "+++ main: ${MAKE_MOTE}${NODEID_GWAY}" 
        ${MAKE_MOTE}${NODEID_GWAY}
    fi 
    
    if [ $TEMP -eq 1 ]; then # temperature sensor
        verbose echo  "+++ main: ${MAKE_MOTE}$((ZONEID*NODEID_FACTOR))" 
        ${MAKE_MOTE}$((ZONEID*NODEID_FACTOR))
    fi
    
    if [ $VENT -eq 1 ]; then # vent controller
        verbose echo  "+++ main: ${MAKE_MOTE}$(($ZONEID*$NODEID_FACTOR+$NUM_VENTS_IN_ZONE+1))" 
        ${MAKE_MOTE}$(($ZONEID*$NODEID_FACTOR+$NUM_VENTS_IN_ZONE+1))
    fi  
}

main $@
